<!DOCTYPE html>
<html lang="GB18030" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta name="description" content="">
    <meta name="author" content="">

    <head th:include="common::commonHeader"></head>
    <link rel="stylesheet" th:href="@{/css/main.css}">
    <link rel="stylesheet" th:href="@{/css/doc.min.css}">
    <link rel="stylesheet" th:href="@{/ztree/zTreeStyle.css}">
    <style>
        .tree li {
            list-style-type: none;
            cursor:pointer;
        }
    </style>

</head>

<body>

<div th:replace="main-common::main-body-header"></div>

<div class="container-fluid">
    <div class="row">
        <div th:replace="main-common::main-body-ul"></div>
        <div class="col-sm-9 col-sm-offset-3 col-md-10 col-md-offset-2 main">

            <div class="panel panel-default">
                <div class="panel-heading"><i class="glyphicon glyphicon-th-list"></i> 权限菜单列表 <div style="float:right;cursor:pointer;" data-toggle="modal" data-target="#myModal"><i class="glyphicon glyphicon-question-sign"></i></div></div>
                <div class="panel-body">
                    <!--ztree结点所依附的静态结点-->
                    <ul id="treeDemo" class="ztree"></ul>
                </div>
            </div>
        </div>
    </div>
</div>
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">&times;</span><span class="sr-only">Close</span></button>
                <h4 class="modal-title" id="myModalLabel">帮助</h4>
            </div>
            <div class="modal-body">
                <div class="bs-callout bs-callout-info">
                    <h4>没有默认类</h4>
                    <p>警告框没有默认类，只有基类和修饰类。默认的灰色警告框并没有多少意义。所以您要使用一种有意义的警告类。目前提供了成功、消息、警告或危险。</p>
                </div>
                <div class="bs-callout bs-callout-info">
                    <h4>没有默认类</h4>
                    <p>警告框没有默认类，只有基类和修饰类。默认的灰色警告框并没有多少意义。所以您要使用一种有意义的警告类。目前提供了成功、消息、警告或危险。</p>
                </div>
            </div>
            <!--
            <div class="modal-footer">
              <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
              <button type="button" class="btn btn-primary">Save changes</button>
            </div>
            -->
        </div>
    </div>
</div>
<div th:replace="menu-page-add::menu"></div>
<div th:replace="menu-page-remove::menu"></div>
<div th:replace="menu-page-edit::menu"></div>
<div th:replace="common::commonFooter"></div>
<script th:src="@{/script/docs.min.js}"></script>
<script th:src="@{/ztree/jquery.ztree.all-3.5.min.js}"></script>
<script th:src="@{/crowd/my-menu.js}"></script>
<script>
    $(function () {
        //生成树结构
        generateTree();




        /**
         * 添加
         */
        //给增加子节点添加单击响应事件
        $("#treeDemo").on("click",".addBtn",function () {
            //当当前结点的id作为新节点的pid，表示新增加的结点是当前结点的子节点
            window.pid = this.id;
            $("#menuAddModal").modal("show");
            return false;
        });
        //给模态框的保存按钮绑定单击函数
        $("#menuSaveBtn").click(function () {
            //收集表单数据
            var name = $("#menuAddModal [name=name]").val();
            var url = $("#menuAddModal [name=url]").val();
            var icon = $("#menuAddModal [name=icon]:checked").val();
            $.ajax({
                url:"/menu/saveMenu",
                type:"post",
                dataType:"json",
                data:{
                    "pid":window.pid,
                    "name":name,
                    "url":url,
                    "icon":icon
                },
                success:function (response) {
                    var result = response.result;
                    if (result=="SUCCESS"){
                        console.log("成功");
                        //重新加载树形结构
                        generateTree();
                    }if (result=="FAILED"){
                        console.log("操作失败"+response.message);
                    }
                },
                error:function (response) {
                    console.log(response.status+" "+response.statusText);
                }
            });
            //关闭模块框
            $("#menuAddModal").modal("hide");
            //清空表单
            $("#menuResetBtn").click();
        });




        /**
         * 修改
         */
        //给修改子节点添加单击响应事件
        $("#treeDemo").on("click",".editBtn",function () {
            //当前结点的id
            window.id = this.id;
            $("#menuEditModal").modal("show");
            //查询当前结点信息
            var zTreeObj = $.fn.zTree.getZTreeObj("treeDemo");
            console.log(zTreeObj);
            var key = "id";
            var value = window.id;
            var currentNode = zTreeObj.getNodeByParam(key,value);
            console.log(currentNode);
            //回显
            $("#menuEditModal [name=name]").val(currentNode.name);
            $("#menuEditModal [name=url]").val(currentNode.url);
            $("#menuEditModal [name=icon]").val([currentNode.icon]);

            return false;
        });
        //给模态框的更新按钮绑定单击函数
        $("#menuEditBtn").click(function () {
            //收集表单数据
            var name = $("#menuEditModal [name=name]").val();
            var url = $("#menuEditModal [name=url]").val();
            var icon = $("#menuEditModal [name=icon]:checked").val();

            $.ajax({
                url:"/menu/updateMenu",
                type:"post",
                dataType:"json",
                data:{
                    "id":window.id,
                    "name":name,
                    "url":url,
                    "icon":icon
                },
                success:function (response) {
                    var result = response.result;
                    if (result=="SUCCESS"){
                        console.log("成功");
                        //重新加载树形结构
                        generateTree();
                    }if (result=="FAILED"){
                        console.log("操作失败"+response.message);
                    }
                },
                error:function (response) {
                    console.log(response.status+" "+response.statusText);
                }
            });
            //关闭模块框
            $("#menuEditModal").modal("hide");
        });






        /**
         *  删除
         */
        //给修改子节点添加单击响应事件
        $("#treeDemo").on("click",".removeBtn",function () {
            //当前结点的id
            window.id = this.id;
            $("#menuConfirmModal").modal("show");
            //查询当前结点信息
            var zTreeObj = $.fn.zTree.getZTreeObj("treeDemo");
            var key = "id";
            var value = window.id;
            var currentNode = zTreeObj.getNodeByParam(key,value);
            //回显
            $("#removeNodeSpan").text(currentNode.name);
            return false;
        });
        //给模态框的更新按钮绑定单击函数
        $("#confirmBtn").click(function () {

            $.ajax({
                url:"/menu/removeMenu",
                type:"post",
                dataType:"json",
                data:{
                    "id":window.id,
                },
                success:function (response) {
                    var result = response.result;
                    if (result=="SUCCESS"){
                        console.log("成功");
                        //重新加载树形结构
                        generateTree();
                    }if (result=="FAILED"){
                        console.log("操作失败"+response.message);
                    }
                },
                error:function (response) {
                    console.log(response.status+" "+response.statusText);
                }
            });
            //关闭模块框
            $("#menuConfirmModal").modal("hide");
        });





        $(".list-group-item").click(function(){
            if ( $(this).find("ul") ) {
                $(this).toggleClass("tree-closed");
                if ( $(this).hasClass("tree-closed") ) {
                    $("ul", this).hide("fast");
                } else {
                    $("ul", this).show("fast");
                }
            }
        });

    });
</script>
</body>
</html>
